package com.iqiyi.pps.epg.core.model.control;

import com.iqiyi.pps.epg.api.model.web.broadcast.TProgramPlatformControl;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "program_platform_control")
@Cache(usage = CacheConcurrencyStrategy.NONE)

/**
 * Created by karkarrotxu on 2015/12/24.
 */
public class ProgramPlatformControl implements IPlatformControl, Cloneable {
    public static final int AVAILABLE_STATUS_ALLOW = 1;
    public static final int AVAILABLE_STATUS_DENY = 0;
    private long id;
    private long programId;
    private int playPlatform;
    private int isDownloadAllowed;
    private int isDrm;
    private int isShareAllowed;
    private int isCooperationAllowed;
    private int availableStatus;
    private int rejectSearch;
    private int rejectRecommend;
    private int rejectTop;
    private int liveDelayTime;
    private int ipSegmentDelayTime;
    private String blacklist;
    private Timestamp addTime = new Timestamp(System.currentTimeMillis());
    private Timestamp updateTime;

    private int regionRejectSearch;


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "program_id")
    public long getProgramId() {
        return programId;
    }

    public void setProgramId(long programId) {
        this.programId = programId;
    }

    @Column(name = "play_platform")
    public int getPlayPlatform() {
        return playPlatform;
    }

    public void setPlayPlatform(int playPlatform) {
        this.playPlatform = playPlatform;
    }
    @Column(name = "is_download_allowed")
    public int getIsDownloadAllowed() {
        return isDownloadAllowed;
    }

    public void setIsDownloadAllowed(int isDownloadAllowed) {
        this.isDownloadAllowed = isDownloadAllowed;
    }
    @Column(name = "is_drm")
    public int getIsDrm() {
        return isDrm;
    }

    public void setIsDrm(int isDrm) {
        this.isDrm = isDrm;
    }
    @Column(name = "is_share_allowed")
    public int getIsShareAllowed() {
        return isShareAllowed;
    }

    public void setIsShareAllowed(int isShareAllowed) {
        this.isShareAllowed = isShareAllowed;
    }
    @Column(name = "is_cooperation_allowed")
    public int getIsCooperationAllowed() {
        return isCooperationAllowed;
    }

    public void setIsCooperationAllowed(int isCooperationAllowed) {
        this.isCooperationAllowed = isCooperationAllowed;
    }

    @Column(name = "available_status")
    public int getAvailableStatus() {
        return availableStatus;
    }

    public void setAvailableStatus(int availableStatus) {
        this.availableStatus = availableStatus;
    }

    @Column(name = "reject_search")
    public int getRejectSearch() {
        return rejectSearch;
    }

    public void setRejectSearch(int rejectSearch) {
        this.rejectSearch = rejectSearch;
    }

    @Column(name = "reject_recommend")
    public int getRejectRecommend() {
        return rejectRecommend;
    }

    public void setRejectRecommend(int rejectRecommend) {
        this.rejectRecommend = rejectRecommend;
    }

    @Column(name = "reject_top")
    public int getRejectTop() {
        return rejectTop;
    }

    public void setRejectTop(int rejectTop) {
        this.rejectTop = rejectTop;
    }

    @Column(name = "live_delay_time")
    public int getLiveDelayTime() {
        return liveDelayTime;
    }

    public void setLiveDelayTime(int liveDelayTime) {
        this.liveDelayTime = liveDelayTime;
    }

    @Column(name = "ip_segment_delay_time")
    public int getIpSegmentDelayTime() {
        return ipSegmentDelayTime;
    }

    public void setIpSegmentDelayTime(int ipSegmentDelayTime) {
        this.ipSegmentDelayTime = ipSegmentDelayTime;
    }

    @Column(name = "blacklist")
    public String getBlacklist() {
        return blacklist;
    }

    public void setBlacklist(String blacklist) {
        this.blacklist = blacklist;
    }

    @Column(name = "add_time", updatable = false)
    public Timestamp getAddTime() {
        return addTime;
    }

    public void setAddTime(Timestamp addTime) {
        this.addTime = addTime;
    }

    @Column(name = "update_time" , insertable = false, updatable = false)
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }

    @Column(name = "region_reject_search")
    public int getRegionRejectSearch() {
        return regionRejectSearch;
    }

    public void setRegionRejectSearch(int regionRejectSearch) {
        this.regionRejectSearch = regionRejectSearch;
    }


    public TProgramPlatformControl entityToThrift() {
        TProgramPlatformControl entity = new TProgramPlatformControl();

        entity.setId(this.getId());
        entity.setProgramId(this.getProgramId());
        entity.setPlatform(this.getPlayPlatform());
        entity.setIsDownloadAllowed(this.getIsDownloadAllowed() == 0 ? false : true);
        entity.setIsDrm(this.getIsDrm() == 0 ? false : true);
        entity.setIsShareAllowed(this.getIsShareAllowed() == 0 ? false : true);
        entity.setIsCooperationAllowed(this.getIsCooperationAllowed() == 0 ? false : true);
        entity.setAvailableStatus(this.getAvailableStatus() == 0 ? false : true);
        entity.setRejectSearch(this.getRejectSearch() == 0 ? false : true);
        entity.setRejectRecommend(this.getRejectRecommend() == 0 ? false : true);
        entity.setRejectTop(this.getRejectTop() == 0 ? false : true);
        entity.setLiveDelayTime(this.getLiveDelayTime());
        entity.setIpSegmentDelayTime(this.getIpSegmentDelayTime());
        entity.setBlacklist(this.getBlacklist());
        entity.setRegionRejectSearch(this.getRegionRejectSearch() == 0 ? false : true);
        if (this.getAddTime() != null) {
            entity.setAddTime(DateTimeUtil.transLongFormat(this.getAddTime().getTime()));
        }
        if (this.getUpdateTime() != null) {
            entity.setUpdateTime(DateTimeUtil.transLongFormat(this.getUpdateTime().getTime()));
        }

        return entity;
    }

    public static ProgramPlatformControl thriftToEntity(TProgramPlatformControl entity) {
        ProgramPlatformControl model = new ProgramPlatformControl();
        if (entity.getId() > 0) {
            model.setId(entity.getId());
        }
        model.setProgramId(entity.getProgramId());
        model.setPlayPlatform(entity.getPlatform());
        model.setIsDownloadAllowed(entity.isIsDownloadAllowed() ? 1 : 0);
        model.setIsDrm(entity.isIsDrm() ? 1 : 0);
        model.setIsShareAllowed(entity.isIsShareAllowed() ? 1 : 0);
        model.setIsCooperationAllowed(entity.isIsShareAllowed() ? 1 : 0);
        model.setAvailableStatus(entity.isAvailableStatus() ? 1 : 0);
        model.setRejectSearch(entity.isRejectSearch() ? 1 : 0);
        model.setRejectRecommend(entity.isRejectRecommend() ? 1 : 0);
        model.setRejectTop(entity.isRejectTop() ? 1 : 0);
        model.setLiveDelayTime(entity.getLiveDelayTime());
        model.setIpSegmentDelayTime(entity.getIpSegmentDelayTime());
        model.setBlacklist(entity.getBlacklist());
        model.setRegionRejectSearch(entity.isRegionRejectSearch() ? 1 : 0);
        if (StringUtils.isNotEmpty(entity.getAddTime())) {
            model.setAddTime(Timestamp.valueOf(entity.getAddTime()));
        }
        if (StringUtils.isNotEmpty(entity.getUpdateTime())) {
            model.setUpdateTime(Timestamp.valueOf(entity.getUpdateTime()));
        }

        return model;
    }

    @Override
    public Object clone() {
        ProgramPlatformControl obj = null;
        try {
            obj = (ProgramPlatformControl) super.clone();
        } catch (CloneNotSupportedException e) {
//            e.printStackTrace();
        }
        return obj;
    }
}